package com.gisocn.onemap.tdt;

import android.content.Context;
import android.util.Log;
import com.esri.android.map.TiledServiceLayer;
import com.esri.android.map.event.OnStatusChangedListener;
import com.esri.core.geometry.Envelope;
import com.esri.core.geometry.SpatialReference;
import com.esri.core.internal.io.handler.a;
import com.gisocn.onemap.base.MyApplication;
import com.gisocn.onemap.model.BaseMap;
import com.gisocn.onemap.model.TDTInfo;
import com.gisocn.onemap.model.TDTType;
import com.gisocn.onemap.utils.L;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.concurrent.RejectedExecutionException;

/* loaded from: classes2.dex */
public class TDTTiedMapLayer extends TiledServiceLayer {
    public static String cachepath = MyApplication.getInstances().getExternalFilesDir("") + "/TileTDT";
    private int SRID;
    private TDTInfo _TDTInfo;
    private Context context;
    private BaseMap.InitExtent initExtent;

    public TDTTiedMapLayer(TDTInfo tDTInfo, BaseMap.InitExtent initExtent, int i) {
        super(true);
        if (tDTInfo.getLayerName() != null && tDTInfo.getMapType() == TDTType.LAYER) {
            cachepath = MyApplication.getInstances().getExternalFilesDir("") + "/TileTDT/" + tDTInfo.getLayerName();
        } else {
            cachepath = MyApplication.getInstances().getExternalFilesDir("") + "/TileTDT";
        }
        this._TDTInfo = tDTInfo;
        L.e("TDTTiedMapLayer" + tDTInfo.getMaxZoomLevel());
        this.initExtent = initExtent;
        this.SRID = i;
        init();
    }

    private void init() {
        try {
            getServiceExecutor().submit(new Runnable() { // from class: com.gisocn.onemap.tdt.TDTTiedMapLayer.1
                @Override // java.lang.Runnable
                public void run() {
                    TDTTiedMapLayer.this.initLayer();
                }
            });
        } catch (RejectedExecutionException e) {
            Log.e("TDT Map Layer", "initialization of the layer failed.", e);
        }
    }

    /* JADX INFO: Access modifiers changed from: protected */
    @Override // com.esri.android.map.TiledServiceLayer, com.esri.android.map.Layer
    public void initLayer() {
        L.e("initLayer");
        if (getID() == 0) {
            L.e("initLayer1");
            this.nativeHandle = create();
            changeStatus(OnStatusChangedListener.STATUS.fromInt(-1000));
            return;
        }
        L.e("initLayer2");
        setTileInfo(new TiledServiceLayer.TileInfo(TDTConstant.ORIGIN_2000, TDTConstant.SCALES, TDTConstant.RESOLUTIONS_2000, TDTConstant.RESOLUTIONS_2000.length, 96, 256, 256));
        setFullExtent(new Envelope(Double.valueOf(this.initExtent.getXmin()).doubleValue(), Double.valueOf(this.initExtent.getYmin()).doubleValue(), Double.valueOf(this.initExtent.getXmax()).doubleValue(), Double.valueOf(this.initExtent.getYmax()).doubleValue()));
        setDefaultSpatialReference(SpatialReference.create(this.SRID));
        super.initLayer();
    }

    @Override // com.esri.android.map.TiledServiceLayer
    protected byte[] getTile(int i, int i2, int i3) throws Exception {
        L.e("_getTile_level___________" + i);
        File file = new File(cachepath);
        if (!file.exists()) {
            file.mkdir();
        }
        if (i > this._TDTInfo.getMaxZoomLevel() || i < this._TDTInfo.getMinZoomLevel()) {
            L.e("level > _TDTInfo.getMaxZoomLevel() || level < _TDTInfo.getMinZoomLevel()");
            return new byte[0];
        }
        byte[] offLineCacheFile = getOffLineCacheFile(this._TDTInfo.getMapType().toString(), i, i2, i3);
        try {
            if (offLineCacheFile == null) {
                offLineCacheFile = a.a(new TDTUrl(i, i2, i3, this._TDTInfo.getMapType()).getUrl(this._TDTInfo.getUrl()), new HashMap());
                addOfflineCacheFile(this._TDTInfo.getMapType().toString(), i, i2, i3, offLineCacheFile);
            } else {
                L.e("getOffLineCacheFile!=null");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return offLineCacheFile;
    }

    private byte[] getOffLineCacheFile(String str, int i, int i2, int i3) throws IOException {
        File file = new File(cachepath + "/" + str + "/" + i + "/" + i2 + "/" + i3 + ".dat");
        if (!file.exists()) {
            return null;
        }
        FileInputStream fileInputStream = new FileInputStream(file);
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(1024);
        byte[] bArr = new byte[1024];
        while (true) {
            int read = fileInputStream.read(bArr);
            if (read != -1) {
                byteArrayOutputStream.write(bArr, 0, read);
            } else {
                fileInputStream.close();
                return byteArrayOutputStream.toByteArray();
            }
        }
    }

    private void addOfflineCacheFile(String str, int i, int i2, int i3, byte[] bArr) {
        File file = new File(cachepath);
        if (!file.exists()) {
            file.mkdir();
        }
        File file2 = new File(cachepath + "/" + str);
        if (!file2.exists()) {
            file2.mkdir();
        }
        File file3 = new File(cachepath + "/" + str + "/" + i);
        if (!file3.exists()) {
            file3.mkdir();
        }
        File file4 = new File(cachepath + "/" + str + "/" + i + "/" + i2);
        if (!file4.exists()) {
            file4.mkdir();
        }
        File file5 = new File(cachepath + "/" + str + "/" + i + "/" + i2 + "/" + i3 + ".dat");
        if (file5.exists()) {
            return;
        }
        try {
            FileOutputStream fileOutputStream = new FileOutputStream(file5);
            fileOutputStream.write(bArr);
            fileOutputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
